﻿using System;
using System.Collections.Generic;
using System.Linq;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.App.SSO;
using OpenAuth.Repository.Domain;


namespace OpenAuth.App
{
    public class WorkNoteApp : BaseApp<WorkNote>
    {
        public RevelanceManagerApp ReleManagerApp { get; set; }

        /// <summary>
        /// 加载列表
        /// </summary>
        public TableData Load(QueryWorkNoteListReq request)
        {
            request.limit = 100;
           var worknotes= Repository.Find(request.page, request.limit, "Id desc");
            var worknotesviews = new List<WorkNotesView>();
            foreach (var worknote in worknotes.ToList())
            {
                WorkNotesView workNotesView = new WorkNotesView();
                workNotesView.Id = worknote.Id;
                workNotesView.UserId = worknote.UserId;
                workNotesView.Content = worknote.Content;
                workNotesView.CreateTime = worknote.CreateTime;
                workNotesView.UpdateTime = worknote.UpdateTime;
                workNotesView.Remark = worknote.Remark;
                workNotesView.UserName = UnitWork.Find<User>(u=>u.Id== worknote.UserId).FirstOrDefault().Name; 
                worknotesviews.Add(workNotesView);
            }
            return new TableData
            {
                count = Repository.GetCount(null),
                data = worknotesviews.ToList()
            };
        }
        /// <summary>
        /// 分组显示所有人员
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public TableData LoadByGroup(QueryWorkNoteListReq request)
        {
            string cascadeId = ".0.";
            DateTime createTime =Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
            if (!string.IsNullOrEmpty(request.orgId))
            {
                var org = UnitWork.Find<Org>(u => u.Id == request.orgId).SingleOrDefault();
                cascadeId = org.CascadeId;
            }
            if (!string.IsNullOrEmpty(request.CreateTime))
            {
                createTime = Convert.ToDateTime(request.CreateTime);
            }
            
            var orgList = UnitWork.Find <Org>(u => u.CascadeId.Contains(cascadeId)).ToList();
            //var userIds = ReleManagerApp.Get(Define.USERORG, false, ids);
            

            List<WorkNotesOrg> workNotesOrgList = new List<WorkNotesOrg>();
            foreach (var item in orgList)
            {
                WorkNotesOrg workNotesOrg = new WorkNotesOrg();
                workNotesOrg.OrgName = item.Name;
                var ids = UnitWork.Find<Org>(u => u.CascadeId.Equals(item.CascadeId)).Select(u=>u.Id).ToArray();
                var users= ReleManagerApp.Get(Define.USERORG, false, ids);
                List<WorkNotesView> workNoteslist = new List<WorkNotesView>();
                foreach (var userId in users)
                {
                    WorkNotesView workNotesView = new WorkNotesView();

                    //var workNotes = Repository.Find(u => u.UserId == userId&&u.CreateTime.ToString("yyyy-MM-dd")==request.CreateTime).OrderByDescending(u => u.CreateTime).FirstOrDefault();
                    var workNotes = Repository.Find(u => u.UserId == userId&&u.CreateTime== createTime).OrderByDescending(u => u.CreateTime).FirstOrDefault();
                    if (workNotes != null)
                    {
                        workNotesView.Content = workNotes.Content;
                        workNotesView.UserId = workNotes.UserId;
                        workNotesView.UserName = UnitWork.Find<User>(u => u.Id == workNotes.UserId).FirstOrDefault().Name;
                        workNotesView.CreateDate = workNotes.CreateTime.ToString("yyyy-MM-dd");
                        workNotesView.Remark = workNotes.Remark;
                        workNoteslist.Add(workNotesView);
                    }
                    else
                    {
                        int userCount = UnitWork.Find<User>(u => u.Id == userId).Count();
                        if (userCount > 0)
                        {
                            workNotesView.Content = "";
                            workNotesView.UserId = userId;
                            workNotesView.UserName = UnitWork.Find<User>(u => u.Id == userId).FirstOrDefault().Name;
                            workNotesView.CreateDate = createTime.ToString("yyyy-MM-dd");
                            workNotesView.Remark = "";
                            workNoteslist.Add(workNotesView);
                        }
                    }
                    
                }
                workNotesOrg.data = workNoteslist;
                workNotesOrgList.Add(workNotesOrg);
            }

            TableData tableData = new TableData();
            tableData.code = 1;
            tableData.data = workNotesOrgList;

            return tableData;
        }

        public TableData LoadlistSum(QueryWorkNoteListReq request)
        {
            string cascadeId = ".0.";
            DateTime createTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
            if (!string.IsNullOrEmpty(request.orgId))
            {
                var org = UnitWork.Find<Org>(u => u.Id == request.orgId).SingleOrDefault();
                cascadeId = org.CascadeId;
            }
            if (!string.IsNullOrEmpty(request.CreateTime))
            {
                createTime = Convert.ToDateTime(request.CreateTime);
            }

            var orgList = UnitWork.Find<Org>(u => u.CascadeId.Contains(cascadeId)).ToList();
            //var userIds = ReleManagerApp.Get(Define.USERORG, false, ids);


            List<WorkNotesOrgDate> workNotesOrgList = new List<WorkNotesOrgDate>();
            List<DateTime> datalist = new List<DateTime>();
            TimeSpan ts = Convert.ToDateTime(request.SearchEndDate) - Convert.ToDateTime(request.SearchDate);
            for (int i = 0; i <= ts.Days; i++)
            {
                DateTime date = Convert.ToDateTime(request.SearchDate).AddDays(i);
                datalist.Add(date);
            }

            foreach (var date in datalist)
            {
                WorkNotesOrgDate workNotesOrgDate = new WorkNotesOrgDate();
                List<WorkNotesView> workNoteslist = new List<WorkNotesView>();
                createTime = date;
                foreach (var item in orgList)
                {
                  
                    workNotesOrgDate.OrgName = item.Name;
                    var ids = UnitWork.Find<Org>(u => u.CascadeId.Equals(item.CascadeId)).Select(u => u.Id).ToArray();
                    var users = ReleManagerApp.Get(Define.USERORG, false, ids);
                   
                    foreach (var userId in users)
                    {
                        WorkNotesView workNotesView = new WorkNotesView();

                        //var workNotes = Repository.Find(u => u.UserId == userId&&u.CreateTime.ToString("yyyy-MM-dd")==request.CreateTime).OrderByDescending(u => u.CreateTime).FirstOrDefault();
                        var workNotes = Repository.Find(u => u.UserId == userId && u.CreateTime == createTime).OrderByDescending(u => u.CreateTime).FirstOrDefault();
                        if (workNotes != null)
                        {
                            workNotesView.Content = workNotes.Content == "null" || string.IsNullOrEmpty(workNotes.Content) ? "" : workNotes.Content;
                            workNotesView.UserId = workNotes.UserId;
                            workNotesView.UserName = UnitWork.Find<User>(u => u.Id == workNotes.UserId).FirstOrDefault().Name;
                            workNotesView.CreateDate = workNotes.CreateTime.ToString("yyyy-MM-dd");
                            workNotesView.Remark =workNotes.Remark=="null"||string.IsNullOrEmpty(workNotes.Remark) ?"": workNotes.Remark;
                            workNoteslist.Add(workNotesView);
                        }
                        else
                        {
                            int userCount = UnitWork.Find<User>(u => u.Id == userId).Count();
                            if (userCount > 0)
                            {
                                workNotesView.Content = "";
                                workNotesView.UserId = userId;
                                workNotesView.UserName = UnitWork.Find<User>(u => u.Id == userId).FirstOrDefault().Name;
                                workNotesView.CreateDate = createTime.ToString("yyyy-MM-dd");
                                workNotesView.Remark = "";
                                workNoteslist.Add(workNotesView);
                            }
                        }

                    } 
                }
                workNotesOrgDate.CreateDate = date.ToString("yyyy-MM-dd")+"-"+ System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(date.DayOfWeek).ToString();
                workNotesOrgDate.data = workNoteslist;
                workNotesOrgList.Add(workNotesOrgDate);
            }

            TableData tableData = new TableData();
            tableData.code = 1;
            tableData.data = workNotesOrgList;

            return tableData;
        }

        public void Add(WorkNote obj)
        {
            Repository.Add(obj);
        }
        
        public void Update(WorkNote obj)
        {
            UnitWork.Update<WorkNote>(u => u.Id == obj.Id, u => new WorkNote
            {
               //todo:要修改的字段赋值
            });

        }

        public void AddOrUpdate(WorkNote obj)
        {
            int wcount = Repository.Find(w => w.CreateTime == obj.CreateTime && w.UserId == obj.UserId).Count();
            if (wcount == 0)
            {
                Repository.Add(obj);
            }
            else
            {
                UnitWork.Update<WorkNote>(u => u.CreateTime == obj.CreateTime&&u.UserId== obj.UserId, u => new WorkNote
                {
                    Content=obj.Content,
                    CreateTime=obj.CreateTime,
                    UpdateTime=DateTime.Now,
                    Remark =obj.Remark

                });
            }

        }

    }
}